package delivery.order.service.config;

import jakarta.annotation.PreDestroy;
import org.jodconverter.core.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class ShutdownConfig {
    private static final Logger logger = LoggerFactory.getLogger(ShutdownConfig.class);

    @Qualifier("OfficeManager")
    private final OfficeManager officeManager;

    public ShutdownConfig(OfficeManager officeManager) {
        this.officeManager = officeManager;
    }

    @PreDestroy
    public void onDestroy() {
        logger.warn("应用程序正在关闭，执行清理操作...");
        closeResources();
    }

    private void closeResources() {
        // 关闭数据库连接、文件句柄等资源
        if (officeManager != null && officeManager.isRunning()) {
            try {
                officeManager.stop();
            } catch (Exception e) {
                logger.warn("关闭 OfficeManager 时出错: {}", e.getMessage());
            }
        }
    }

}
